package ldkxingzhe.ldkxingzhedemo.material;

import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ldkxingzhe.ldkxingzhedemo.R;

/**
 * Created by xingzhe on 15/11/16.
 */
public class AppBarLayoutActivity extends AppCompatActivity{

    private final String TAG = AppCompatActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_appbar_layout);
        RecyclerView listView = (RecyclerView)findViewById(R.id.recycler_view);
        TextViewAdapter adapter = new TextViewAdapter();
        for(int i = 0; i < 50; ++i){
            adapter.add(String.valueOf(i));
        }
        listView.setLayoutManager(new LinearLayoutManager(this));
        listView.setAdapter(adapter);

        CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing_bar);
        collapsingToolbarLayout.setTitle("明月几十有");
    }

    class TextViewAdapter extends RecyclerView.Adapter{
        private final String TAG = TextViewAdapter.class.getSimpleName();
        private List<String> nameList = new ArrayList<>();

        public void add(String name){
            nameList.add(name);
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(AppBarLayoutActivity.this)
                    .inflate(android.R.layout.simple_list_item_1, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            MyViewHolder myViewHolder = (MyViewHolder)holder;
            myViewHolder.textView.setText(nameList.get(position));
        }

        @Override
        public int getItemCount() {
            return nameList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder{
            private final String TAG = MyViewHolder.class.getSimpleName();

            TextView textView;
            public MyViewHolder(View itemView) {
                super(itemView);
                textView = (TextView)itemView;
            }
        }
    }
}
